﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICeFramework.Common.DataLayer;
using System.Data;
using ICeFramework.DataAccess.DataLayer.Enums;

namespace ICeFramework.DataAccess.DataLayer.MsSQL
{
    public class SelectCommand : DBCommand
    {
        public override DBCommandType CommandType
        {
            get { return DBCommandType.Select; }
        }

        public SelectCommand(EntityFilter filter, EntityOrder order, string tableName, IDbTransaction transaction)
            : base(filter, order, tableName, transaction)
        {
        }

        public SelectCommand(string tableName, IDbTransaction transaction) :
            base(tableName, transaction)
        {

        }

        public override string BuildCommandText(string parameterToken)
        {
            if ((object)base.Filter != null)
            {
                base.Filter.SetParameterName(parameterToken);
                _parameters = base.Filter.FilterParameters();
            }

            _commandText = string.Format("SELECT {0} FROM {1}", base.Count ? "COUNT(*)" : "*", _entityName);
            if ((object)base.Filter != null)
            {
                _commandText += string.Format(" WHERE {0}", base.Filter.ToSQLCommand());
            }
            if (base.Order != null && !base.Count)
            {
                _commandText += string.Format(" ORDER BY {0}", base.Order.ToString());
            }

            return _commandText;
        }

        public override string BuildPagingCommandText(string parameterToken, int pageIndex, int pageSize)
        {
            throw new NotImplementedException();
        }
    }
}
